<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Member as MemberModel;
use think\Request;
use think\Db;

class Member extends BaseController
{
    public function list($page = 1,$q='')
    {
        //$student = new StudentModel;
        $q = input('get.q');
        $pageParam = ['query' => []];
        //$list = StudentModel::order('stu_id desc')->paginate(15);
        if($q){
            $pageParam['query']['q'] = $q;
            $list = MemberModel::where('cardno|mobile','LIKE',"%$q%")->order('id desc')->paginate(15,false,$pageParam);            
        }else{
            $list = MemberModel::order('id desc')->paginate(15);
        }
        
        $memberCount = MemberModel::count();

        $this->assign('memberCount',$memberCount);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost())
        {
            $member = new MemberModel;
            if ($member->allowField(true)->validate(true)->save(input('post.'))) {
                //return '用户[ ' . $student->stu_name . ':' . $student->stu_id . ' ]新增成功';
                return redirect('member/list');
            } else {
                //return $student->getError();
                $this->error($member->getError());
            }
            //return redirect('student/list');
        }
        return $this->fetch();
    }

    public function edit($id = 0,$page = 1,$q='')
    {
        $member = MemberModel::get($id);
        if($member)
        {
            $this->assign('member',$member);
        } else {
            echo '非法参数！';die;
        }

        if(request()->isPost())
        {
            if ($member->allowField(true)->validate(true)->save(input('post.'))) {
                //return '用户[ ' . $student->stu_name . ':' . $student->stu_id . ' ]更新成功';
                return redirect('member/list',['page' => $page,'q' => $q]);
                //$this->success('更新成功');
            } else {
                //return $student->getError();
                $this->error($member->getError());
            }
        }
        $this->assign('page',$page);
        //$this->assign('q',$q);
        return $this->fetch();
    }

    // 删除数据
    public function del($id = 0)
    {
        $member = MemberModel::get($id);
        if($member)
        {
            $this->assign('member',$member);
        } else {
            echo '非法参数！';die;
        }

        if(Request()->isPost())
        {
            $result = MemberModel::destroy($id);
            if ($result) {
                return redirect('member/list');
            } else {
                $this->error('删除用户错误！');
            }
        }

        return $this->fetch();
        
    }

    //清空数据
    public function empty()
    {
        if(Request()->isPost())
        {
            // 清空数据表
            $result = Db::execute('TRUNCATE table wx_member');
            if ($result===0) {
                return redirect('member/list');
            } else {
                $this->error('操作错误！');
            }
        }

        return $this->fetch();
        
    }

    public function import(Request $request)
    {
        if(request()->isPost())
        {
           // 获取表单上传文件
            $file = $request->file('excelTable');
            // 上传文件验证
            $result = $this->validate(['file' => $file], ['file'=>'require|fileExt:xls,xlsx'],['file.require' => '请选择上传文件', 'file.fileExt' => '非法excel文件']);
            if(true !== $result){
                $this->error($result);
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel');
            if ($info) {
                //$this->success('文件上传成功：' . $info->getRealPath());die;
                //读取excel数据写入数据库
                $data = importExcel($info->getRealPath(),$info->getExtension());
                //dump($data);die;
                foreach ($data as $key => $val) {
                    if ($key > 1) {
                        $datas['cardno'] = $val['A'];
                        $datas['mobile'] = $val['B'];

                        $newData[] = $datas;
                    }
                }
                $member = new MemberModel;
                $result = $member->saveAll($newData);
                //dump($result);die;
                return redirect('member/list');
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            } 
        }

        return $this->fetch();
    }

    public function export()
    {
        if(request()->isPost())
        {
            //导出数据
            // excel表头  
            $headArr[] = '会员编号';
            $headArr[] = '手机号码';
            // 数据
            $list = MemberModel::select();
            $data = array();
            foreach($list as $memeber){
                // 获取学生选课数据
                $datatemp[0]=$memeber->cardno;
                $datatemp[1]=$memeber->mobile;

                $data[] = $datatemp;
            }
            //dump($data);die;
            $filename = '会员信息表';
            if($data){
                exportExcel($filename, $headArr, $data); // $filename excel名称 $headArr excel表头
            }            
        }
        return $this->fetch();
    }
}